/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-03-28 13:48:51
 * @LastEditTime : 2023-05-05 15:15:10
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Modal\MoreModal\AttachmentList.jsx
 * @Description  :
 */
import { Http } from '../../Api'
import { useBoolean, useRequest } from 'ahooks'
import { useEffect, useState } from 'react'
import { List, Button, message } from 'antd'
import { SeeEnclosureModal } from '../Modal'

export const AttachmentList = ({ data }) => {
    const [messageApi, contextHolder] = message.useMessage()
    const { runAsync } = useRequest(Http, { manual: true })
    const { runAsync: deleteData } = useRequest(Http, {
        manual: true,
        onSuccess: () => {
            messageApi.success('删除成功!')
            runAsync({ url: data.get, data: { ProjectCompanyMonthReportUuid: data.uuid } }).then(res =>
                setList(res.list),
            )
        },
    })

    const [Data, setList] = useState([])
    useEffect(() => {
        runAsync({ url: data.get, data: { ProjectCompanyMonthReportUuid: data.uuid } }).then(res => setList(res.list))
    }, [data, runAsync])

    const [FileData, setFileData] = useState()
    const [FileStatus, { setTrue, setFalse }] = useBoolean(false)

    return (
        <>
            {contextHolder}
            <List
                bordered
                dataSource={Data}
                renderItem={({ uuid, tableName, attachment }) => (
                    <List.Item>
                        <div
                            onClick={() => {
                                setFileData(attachment.fileUrl)
                                setTrue()
                            }}
                            style={{ cursor: 'pointer', flex: 1 }}
                        >
                            {tableName}
                        </div>
                        <Button type="link" danger onClick={() => deleteData({ url: data.del, path: [uuid] })}>
                            删除
                        </Button>
                    </List.Item>
                )}
            />
            {FileStatus && <SeeEnclosureModal data={{ className: 'Admin', url: FileData }} Off={setFalse} />}
        </>
    )
}
